package com.xuzm.spark

import org.apache.spark.rdd.RDD
import org.apache.spark.storage.StorageLevel
import org.apache.spark.{SparkConf, SparkContext}

object Spark_cache {
  def main(args: Array[String]): Unit = {
    //1.创建SparkConf并设置App名称
    val conf = new SparkConf().setAppName("Spark_cache").setMaster("local[*]")

    //2.创建SparkContext，该对象是提交Spark App的入口
    val sc = new SparkContext(conf)

    val rdd:RDD[String] = sc.makeRDD(List("hello spark", "spark cache"),2)

    val flatMapRDD: RDD[String] = rdd.flatMap(_.split(" "))

    val mapRDD: RDD[(String, Int)] = flatMapRDD.map(word => {
      println("********");
      (word, 1)
    })
    mapRDD.collect()
    println(mapRDD.toDebugString)
    println("================================")
//    mapRDD.cache() // 缓存在内存中，实质即使调用的persist
//    mapRDD.persist(StorageLevel.DISK_ONLY)  // 存入磁盘

    println(mapRDD.toDebugString)

//    println(mapRDD.)

    sc.stop()
  }
}
